#include "testing.h"
#include "ui_testing.h"

Testing::Testing(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Testing)
{
    ui->setupUi(this);
    //键盘初始化
    ui->lineEdit->setFocus();
    keyboard->initFocusWidget(ui->widget);
    keyboard->hide();
    isKeyBoardShow = 0;
    //输入框初始化
    ui->itemname->clear();
    ui->lineEdit->clear();
    //抗锯齿
    chartView->setRenderHint(QPainter::Antialiasing);
    //隐藏点标识
    chart->legend()->hide();
    //初始图像占据一块区域
    ui->verticalLayout->addWidget(chartView);
    //ui美化
    this->initStyleSheet();
    //close keyboard
    connect(keyboard,&frmKeyBoard::hiding,this,&Testing::hidekeyboard);
}

Testing::~Testing()
{
    delete ui;
}

void Testing::initStyleSheet()
{
    QString qss;

    qss += "QLineEdit{border-width: 1px; border-radius: 10px; border-style: solid; border-color: rgb(98,183,152); }";
    qss += "#chartView{background-color:rgb(98,183,152);}";
    qss += "#itemname{border-color:rgb(193,150,144);}";
    qss += "#vector2{border-color:rgb(193,150,144);}";
    /*
    qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
    qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
    qss += "#pushButton_space{min-width:500px;}";
    qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
    qss += "#pushButton_enter{min-width:120px;}";
    qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
    qss += "#pushButton_capsLock{min-width:80px;}";
    qss += "#pushButton_up{min-width:150px;}";
    */
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}

//键盘显示与隐藏
void Testing::on_keyboard_clicked()
{
    if(isKeyBoardShow == 0){
        keyboard->setGeometry(0, 175, 431, 166);
        keyboard->show();
        isKeyBoardShow = 1;
    }else{
        keyboard->hide();
        isKeyBoardShow = 0;
    }
}

//item录入
void Testing::on_start_clicked()
{
    ItemManager itemManager;
    if(barcode.isEmpty()){
        QMessageBox::warning(this,tr("failed to inroll"),tr("please get barcode first!"),QMessageBox::Ok);
    }else if(itemManager.checkItemExists(barcode)){
        QMessageBox::warning(this,tr("failed to inroll"),tr("this item has been inrolled before"),QMessageBox::Ok);
        //指针作为一个不可能为空，会指向某个文本框，所以用ui->itemname == NULL是错误的判断方法
    }else if(ui->itemname->text().isEmpty()){
        QMessageBox::warning(this,tr("failed to inroll"),tr("please write item name first"),QMessageBox::Ok);
    }else{
        Item item;
        MethodManager methodManager;
        Method method = methodManager.getMethod(methodname);
        item.barcode = barcode;
        item.itemName = ui->itemname->text();
        item.createdBy = Permission::User;
        //传送item相关method的信息
        item.methodName = method.methodName;
        item.lightIntensity = method.lightIntensity;
        item.integrationTime = method.integrationTime;
        item.integrationCount = method.integrationCount;
        item.displacement = method.displacement;
        //创建该item
        bool result = itemManager.createItem(item);
        if (result) {
            qDebug() << "Item created successfully";
            QMessageBox::warning(this,tr("failed"),tr("verify failed！"),QMessageBox::Ok);
            QMessageBox::warning(this,tr("success"),tr("enroll item success！"),QMessageBox::Ok);
            Permission::LastPage = 2;//记录本次页面index,使得下个页面点击返回可直接返回到该页面
            emit reset();

            //生成图像，得到每一个点
            QString array = ui->vector->text();
            QStringList stringList = array.split(" ");
            QVector<double> data;
            for (const QString& str : stringList) {
                double number = str.toDouble();
                data.append(number);
            }
            QString array2 = ui->vector2->text();
            QStringList stringList2 = array2.split(" ");
            QVector<double> data2;
            for (const QString& str : stringList2) {
                double number = str.toDouble();
                data2.append(number);
            }
            //初始化
            chart->removeSeries(series);
            series->clear();
            //将点记录
            int size = 0;
            if(data.size() <= data2.size()){
                size = data.size();
            }else{
                size = data2.size();
            }
            for (int i = 0; i < size; ++i) {
                double x = data[i];
                double y = data2[i];
                series->append(x, y);
            }
            //将记录的点放在chart上
            chart->addSeries(series);
            chart->createDefaultAxes();
            ui->verticalLayout->addWidget(chartView);
        } else {
            QMessageBox::warning(this,tr("failed"),tr("item inroll failed！"),QMessageBox::Ok);
            qDebug() << "Failed to create item";
        }
    }
    //每次录入后清除
    //ui->itemname->clear(); //itemname始终保留
    ui->lineEdit->clear();
    barcode.clear();
    ui->textBrowser->clear();
}


void Testing::on_home_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    Permission::LastPage = 2;
    emit display(5); //l2
}


void Testing::on_methoddevelop_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(3); //m
}


void Testing::on_selectmethod_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(1); //s
}


void Testing::on_settings_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(4); //set
}

//提取该methodName,为了让start处理
void Testing::nameRecevier(QString methodName)
{
    methodname = methodName;
}

//管理员相关管理
void Testing::hiding(bool s)
{
    if(s==0){
        ui->settings->hide();
    }else{
        ui->settings->show();
    }
}

//暂时通过输入框替代二维码的扫描
void Testing::on_scanQRcode_clicked()
{
    ui->textBrowser->clear();//每次摁下后自动清空屏幕
    ItemManager itemManager;
    //barcode = HardwareManager::ScanQRCode();//扫码二维码,返回字符串,为创建的item写入barcode
    //将barcode返回到编辑栏（且此栏无法被编辑修改）
    //ui->lineEdit->setText(barcode);
    barcode = ui->lineEdit->text();//暂时替代
    if(barcode.isEmpty()){
    }else if(!itemManager.checkItemExists(barcode)){
        ui->textBrowser->insertPlainText("barcode:"+barcode+"\n"+"Item does not exist");
    }else{
        Item item = itemManager.getItemByUser(barcode, Permission::User);
        ui->textBrowser->insertPlainText("barcode:\n"+item.barcode+"\n");
        ui->textBrowser->insertPlainText("createdBy:\n"+item.createdBy+"\n");
        ui->textBrowser->insertPlainText("creationTime:\n"+item.creationTime.toString()+"\n");
        // Convert QVector<int> to QString with a separator
        QStringList stringList;
        for (int i : item.displacement) {
            stringList.append(QString::number(i));
        }
        QString result = stringList.join(" ");
        ui->textBrowser->insertPlainText("result:\n"+result+"\n");
        ui->textBrowser->insertPlainText("integrationCount:\n"+QString::number(item.integrationCount)+"\n");
        ui->textBrowser->insertPlainText("integrationTime:\n"+QString::number(item.integrationTime)+"\n");
        ui->textBrowser->insertPlainText("itemName:\n"+item.itemName+"\n");
        ui->textBrowser->insertPlainText("lightIntensity:\n"+QString::number(item.lightIntensity)+"\n");
        ui->textBrowser->insertPlainText("methodName:\n"+item.methodName+"\n");
    }
}

//hide keyboard by keyboard's button
void Testing::hidekeyboard()
{
    keyboard->hide();
    isKeyBoardShow = 0;
}
